﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CL2018_1.Model;

namespace CL2018_1.UI
{
    /// <summary>
    /// UI_Config.xaml 的交互逻辑
    /// </summary>
    public partial class UI_Config : UserControl
    {
        private Model.Config config;
        private ComboBox[] rs485CBB = null;

        public UI_Config()
        {
            InitializeComponent();
            this.cbbBws.ItemsSource = new int[] { 6, 12, 24, 48, 96 };
        }

        internal UI_Config(Model.Config config)
            : this()
        {
            //初始化界面数据
            this.config = config;
            if (this.config.TaiType == Model.TaiType.单相台)
            {
                this.rbDan.IsChecked = true;
            }
            else
            {
                this.rbSan.IsChecked = true;
            }

            this.cbbBws.SelectedItem = this.config.Bws;
            this.cb188H.IsChecked = this.config.Is188H;
            this.cbbChannelCount.SelectedItem = this.config.Rs485Count;
            this.tb2018_1Address.Text = this.config.CL2018_1Address;
            this.tb303Port.Text = this.config.Power303Port.ToString();
            this.tbWcb.Text = Model.Config.ArrayToString(this.config.Port_188);
        }

        public bool Save()
        {
            if (this.rbDan.IsChecked == true)
            {
                this.config.TaiType = Model.TaiType.单相台;
            }
            else
            {
                this.config.TaiType = Model.TaiType.三相台;
            }
            this.config.Is188H = this.cb188H.IsChecked.Value;
            this.config.Bws = int.Parse(this.cbbBws.SelectedItem.ToString());
            this.config.Rs485Count = int.Parse(this.cbbChannelCount.SelectedItem.ToString());
            this.config.CL2018_1Address = this.tb2018_1Address.Text.Trim();
            this.config.Power303Port = int.Parse(this.tb303Port.Text.Trim());
            this.config.Port_188 = Model.Config.StringToIntArray(this.tbWcb.Text.Trim());

            this.config.RS485Ports.Clear();

            for (int i = 0; i < this.rs485CBB.Length; i++)
            {
                this.config.RS485Ports.Add(i + 1, int.Parse(this.rs485CBB[i].SelectedItem.ToString().Substring(2)));
            }


            return this.config.Save();
        }

        /// <summary>
        /// 表位数，选择事件，
        /// 自动计算 485通道数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbbBws_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.cbbChannelCount.SelectedItem = null;
            this.cbbChannelCount.Items.Clear();

            for (int i = 0; i < int.Parse(this.cbbBws.SelectedItem.ToString()); i++)
            {
                this.cbbChannelCount.Items.Add(i + 1);
            }
        }


        /// <summary>
        /// 485通道数选择事件，处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbbChannelCount_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbbChannelCount.SelectedItem == null)
            {
                this.ugRs485Ports.Children.Clear();
                return;
            }

            int rs485Count = int.Parse(this.cbbChannelCount.SelectedItem.ToString());

            string[] TmpArr = new string[64];

            for (int i = 0; i < 64; i++)
            {
                TmpArr[i] = string.Format("通道{0:D2}", i + 1);
            }
            this.ugRs485Ports.Children.Clear();
            this.rs485CBB = new ComboBox[rs485Count];

            for (int i = 0; i < this.rs485CBB.Length; i++)
            {
                rs485CBB[i] = new ComboBox();
                rs485CBB[i].ItemsSource = TmpArr;
                rs485CBB[i].SelectedIndex = i;
                rs485CBB[i].Margin = new Thickness(0, 2, 2, 2);
                rs485CBB[i].Width = 80;
                rs485CBB[i].Height = 20;
                TextBlock txt = new TextBlock();
                txt.Text = string.Format("线路{0:d2}:", i + 1);
                txt.Margin = new Thickness(4, 5, 4, 2);
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                sp.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                sp.Children.Add(txt);
                sp.Children.Add(this.rs485CBB[i]);
                this.ugRs485Ports.Children.Add(sp);
            }
        }
    }
}
